<template>
  <div class="user-management">
    <div class="header-section">
      <h2>用户管理</h2>
      <div class="search-bar">
        <el-input
          v-model="searchForm.username"
          placeholder="用户名"
          class="search-input"
        />
        <el-input
          v-model="searchForm.realname"
          placeholder="真实姓名"
          class="search-input"
        />
        <el-select
          v-model="searchForm.status"
          placeholder="状态"
          class="search-select"
        >
          <el-option label="全部" value="" />
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </div>
    </div>

    <div class="table-section">
      <el-table :data="userList" style="width: 100%">
        <el-table-column prop="id" label="序号" width="80" />
        <el-table-column prop="username" label="用户名" width="150" />
        <el-table-column prop="realname" label="真实姓名" width="150" />
        <el-table-column prop="department" label="部门" width="150" />
        <el-table-column prop="position" label="职位" width="120" />
        <el-table-column prop="email" label="邮箱" width="200" />
        <el-table-column prop="phone" label="电话" width="150" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              :type="scope.row.status === 1 ? 'danger' : 'success'"
              size="small"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 1 ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 添加/编辑用户对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-form :model="formData" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="formData.realname" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="formData.department" placeholder="请选择部门">
            <el-option label="市场部" value="市场部" />
            <el-option label="技术部" value="技术部" />
            <el-option label="财务部" value="财务部" />
            <el-option label="人力资源部" value="人力资源部" />
            <el-option label="客服部" value="客服部" />
          </el-select>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="formData.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="formData.status"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>

    <!-- 批量操作 -->
    <div class="batch-operations">
      <el-button type="primary" @click="handleAddUser">添加用户</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';

// 搜索表单
const searchForm = reactive({
  username: '',
  realname: '',
  status: ''
});

// 分页信息
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 100
});

// 对话框状态
const dialogVisible = ref(false);
const dialogTitle = ref('添加用户');
const formData = reactive({
  id: '',
  username: '',
  realname: '',
  department: '',
  position: '',
  email: '',
  phone: '',
  status: '1'
});

// 模拟用户数据
const userList = ref([
  {
    id: 1,
    username: 'admin',
    realname: '管理员',
    department: '管理员',
    position: '管理员',
    email: 'admin@example.com',
    phone: '13800138000',
    status: 1,
    createTime: '2023-01-01 00:00:00'
  },
  {
    id: 2,
    username: 'user1',
    realname: '张三',
    department: '市场部',
    position: '经理',
    email: 'zhangsan@example.com',
    phone: '13800138001',
    status: 1,
    createTime: '2023-01-02 10:00:00'
  },
  {
    id: 3,
    username: 'user2',
    realname: '李四',
    department: '技术部',
    position: '开发工程师',
    email: 'lisi@example.com',
    phone: '13800138002',
    status: 1,
    createTime: '2023-01-03 14:30:00'
  },
  {
    id: 4,
    username: 'user3',
    realname: '王五',
    department: '财务部',
    position: '会计',
    email: 'wangwu@example.com',
    phone: '13800138003',
    status: 1,
    createTime: '2023-01-04 09:15:00'
  },
  {
    id: 5,
    username: 'user4',
    realname: '赵六',
    department: '人力资源部',
    position: '专员',
    email: 'zhaoliu@example.com',
    phone: '13800138004',
    status: 1,
    createTime: '2023-01-05 16:45:00'
  },
  {
    id: 6,
    username: 'user5',
    realname: '孙七',
    department: '客服部',
    position: '客服专员',
    email: 'sunqi@example.com',
    phone: '13800138005',
    status: 0,
    createTime: '2023-01-06 11:20:00'
  },
  {
    id: 7,
    username: 'user6',
    realname: '周八',
    department: '市场部',
    position: '市场专员',
    email: 'zhouba@example.com',
    phone: '13800138006',
    status: 1,
    createTime: '2023-01-07 15:10:00'
  },
  {
    id: 8,
    username: 'user7',
    realname: '吴九',
    department: '技术部',
    position: '测试工程师',
    email: 'wujiu@example.com',
    phone: '13800138007',
    status: 1,
    createTime: '2023-01-08 10:30:00'
  },
  {
    id: 9,
    username: 'user8',
    realname: '郑十',
    department: '财务部',
    position: '财务经理',
    email: 'zhengshi@example.com',
    phone: '13800138008',
    status: 1,
    createTime: '2023-01-09 14:00:00'
  },
  {
    id: 10,
    username: 'user9',
    realname: '陈十一',
    department: '人力资源部',
    position: 'HR经理',
    email: 'chenshiyi@example.com',
    phone: '13800138009',
    status: 1,
    createTime: '2023-01-10 09:45:00'
  }
]);

// 搜索功能
const handleSearch = () => {
  ElMessage.success('搜索功能已触发');
  // 这里应该调用API进行搜索
};

// 重置搜索
const resetSearch = () => {
  searchForm.username = '';
  searchForm.realname = '';
  searchForm.status = '';
};

// 添加用户
const handleAddUser = () => {
  dialogTitle.value = '添加用户';
  Object.keys(formData).forEach(key => {
    formData[key] = key === 'status' ? '1' : '';
  });
  dialogVisible.value = true;
};

// 编辑用户
const handleEdit = (row) => {
  dialogTitle.value = '编辑用户';
  Object.assign(formData, row);
  dialogVisible.value = true;
};

// 切换用户状态
const handleToggleStatus = (row) => {
  row.status = row.status === 1 ? 0 : 1;
  ElMessage.success(`用户${row.status === 1 ? '启用' : '禁用'}成功`);
};

// 保存用户
const handleSave = () => {
  dialogVisible.value = false;
  ElMessage.success(dialogTitle.value === '添加用户' ? '用户添加成功' : '用户编辑成功');
};

// 分页功能
const handleSizeChange = (size) => {
  pagination.pageSize = size;
};

const handleCurrentChange = (current) => {
  pagination.currentPage = current;
};
</script>

<style scoped>
.user-management {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
}

.header-section {
  margin-bottom: 20px;
}

.header-section h2 {
  margin-bottom: 16px;
  color: #333;
}

.search-bar {
  display: flex;
  gap: 12px;
  align-items: center;
}

.search-input {
  width: 180px;
}

.search-select {
  width: 120px;
}

.table-section {
  margin-bottom: 20px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.batch-operations {
  margin-bottom: 16px;
}
</style>